import {
    userCount,
    userList,
    userInfo,
    roleList
} from "../../request/api"

const state = {
    //管理员总数
    userCount: "",
    //管理员列表
    userList: [],
    //获取一个管理员
    userInfo: null,
    roleList:[]
}
//导出
const getters = {
    userCount(state) {
        return state.userCount
    },
    userList(state) {
        return state.userList
    },
    userInfo(state) {
        return state.userInfo
    },
    roleList(state) {
        return state.roleList
    },
}
//修改
const mutations = {
    cUserCount(state, data) {
        state.userCount = data;
    },
    cUserList(state, data) {
        state.userList = data;
    },
    cUserInfo(state, data) {
        state.userInfo = data;
    },
    cRoleList(state, data) {
        state.roleList = data;
    },
}
// 异步逻辑
const actions = {
    reqUserCount(context) {
        userCount().then(res => {
            context.commit("cUserCount", res.data.list[0].total)
        })
    },
    reqUserList(context, data) {
        userList(data).then(res => {
            context.commit("cUserList", res.data.list?res.data.list:[])
        })
    },
    reqUserInfo(context, data) {
        userInfo(data).then(res => {
            context.commit("cUserInfo", res.data.list?res.data.list:{})
        })
    },
    reqRoleList(context) {
        roleList().then(res => {
            context.commit("cRoleList", res.data.list?res.data.list:[])
        })
    },
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}